package com.woniuxy.xiangruicommonutilserver.timer;

import com.woniuxy.xiangruicommonutilserver.bean.WorkDayInnerBean;
import com.woniuxy.xiangruicommonutilserver.bean.WorkResultBean;
import com.woniuxy.xiangruicommonutilserver.dao.WorkInfoDao;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: masterhai
 * @date: 2021/4/18:19:09
 * @description:
 */
@Slf4j
@Component
public class WorkDayTimerTask {
    @Resource
    private RestTemplate restTemplate;
    @Resource
    private WorkInfoDao workInfoDao;

    //定时任务
    @Scheduled(cron = "0 45 10 * * ?")
    public void updateWorkDayTask () {
        System.out.println("定时任务更新工作日数据执行了");
        //获取当前年份
        String nowDate = new DateTime().toString("yyyy-MM-dd");
        Integer year = Integer.valueOf(nowDate.split("-")[0]);
        Integer nowTime = Integer.valueOf(nowDate.replaceAll("-", ""));
        //删除当天以前的记录
        int i = workInfoDao.deleteInfo();
        //查询并插入当年和下一年的数据
        insertWorkDayInfo2DB(String.valueOf(year));
        insertWorkDayInfo2DB(String.valueOf(year + 1));


    }


    public int insertWorkDayInfo2DB (String year) {
        List<WorkDayInnerBean> workDayInfo = getWorkDayInfo(year);
        //遍历集合,将非工作日的数据移除
        if (workDayInfo == null || workDayInfo.size() < 1) {
            return - 1;
        }
        //创建新集合保存数据
        ArrayList<WorkDayInnerBean> newData = new ArrayList<>();
        log.info("移除非工作日数据前集合大小:{}", workDayInfo.size());
        for (int i = workDayInfo.size() - 1; i >= 0; i--) {
            WorkDayInnerBean bean = workDayInfo.get(i);
            if (bean.getWorkday() == 1) {
                //添加该对象到新集合
                newData.add(bean);
            }
        }
        // log.info("移除非工作日数据后集合大小:{}", newData.size());
        //清空原集合
        workDayInfo.clear();
        //判断当前数据库有无该年交易日数据,有则不插入
        return workInfoDao.insertWorkDayInfo(newData);
    }


    public List<WorkDayInnerBean> getWorkDayInfo (String year) {
        //先拼接月份202101,202102,...,202111
        StringBuffer sb = new StringBuffer();
        for (int i = 1; i < 12; i++) {
            //拼接年份
            sb.append(year);
            int len = String.valueOf(i).length();
            //获取格式化后的月份
            String month = ("0" + i).substring(len - 1, len + 1);
            //拼接逗号
            sb.append(month).append(",");
        }
        //拼接12月份
        String months = sb.append(year).append("12").toString();
        //发送请求获取数据
        String url = "https://api.apihubs.cn/holiday/get?year=" + year + "&month=" + months + "&field=date,workday&size=400";
        log.info("url:{}", url);
        WorkResultBean workResultBean = restTemplate.getForObject(url, WorkResultBean.class);
        return workResultBean.getData().getList();
    }
}
